package com.yeyouluo.writable;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class SpeakReducer extends Reducer<Text, SpeakBean, Text, SpeakBean> {
    @Override
    protected void reduce(Text key, Iterable<SpeakBean> values, Context context) throws IOException, InterruptedException {
        // count init
        Long selfDurationCount = 0L;
        Long thirdPartDurationCount = 0L;

        // sum
        for (SpeakBean speakBean : values) {
            selfDurationCount += speakBean.getSelfDuration();
            thirdPartDurationCount += speakBean.getThirdPartDuration();
        }

        // generate SpeakBean
        SpeakBean speakBean = new SpeakBean(key.toString(), selfDurationCount, thirdPartDurationCount);

        // output
        context.write(key, speakBean);
    }
}
